//const { XRShadow } = require("XrFrame/elements");

// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userwxid:'',
    username:'',
    checked: false,
    userList:[],
    array:[],
  },
  usernameInput(e){
    this.setData({
      username:e.detail.value
    })
  },
  userwxidInput(e){
    this.setData({
      userwxid:e.detail.value
    })
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  onLogin(){
  // 获取当前日期和时间
  var date = new Date();

  // 格式化日期和时间
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();

  // 补零
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hour = hour < 10 ? '0' + hour : hour;
  minute = minute < 10 ? '0' + minute : minute;
  second = second < 10 ? '0' + second : second;

// 拼接字符串
var createtime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

   
    //全局变量
    getApp().globalData.userName = this.data.username;
    getApp().globalData.userWxid = this.data.userwxid;
    var number = 0;
    var user = {wxid: this.data.userwxid,wxName:this.data.username,identity:number,createTime:createtime,status:true};
    console.log(this.data.userwxid);
    var exists = this.data.array.some(user => user.wxid === this.data.userwxid);
    console.log(exists);  // 如果存在，打印 true；否则，打印 false

    if(!exists){
    wx.request({      
      url: 'http://8.219.238.141/user/addUser',
      method: 'POST',
      data: user,
      success: function(res) {
        console.log(res);
        if (res.data.msg === 'success') {
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          });
          setTimeout(function() {
            wx.navigateBack();
          }, 1000); // 1000 毫秒等于 1 秒
        // } else {
        //   wx.showToast({
        //     title: '已存在,登录成功',
        //     icon: 'success'
        //   });
        //   setTimeout(function() {
        //     wx.navigateBack();
        //   }, 1000); // 1000 毫秒等于 1 秒
         }
      }
    });
  }else{
    let user2 = this.data.array.find(user2 => user2.wxid === this.data.userwxid);
    console.log(user2);
    wx.showToast({
          title: '已存在,登录成功',
          icon: 'success'
        });
        setTimeout(function() {
          wx.navigateBack();
        }, 1000); // 1000 毫秒等于 1 秒
  }
  },
  getArray() {
    wx.request({
      url: 'http://8.219.238.141/user/getUserByIdentity?identity=0',
      method: 'POST',
      success: (res) => {
        this.setData({
          array: [...this.data.array, ...res.data.data]
        })
      }
    });
    this.setData({
      array: this.data.array
    });
    wx.request({
      url: 'http://8.219.238.141/user/getUserByIdentity?identity=1',
      method: 'POST',
      success: (res) => {
        this.setData({
          array: [...this.data.array, ...res.data.data]
        })
      }
    });
    this.setData({
      array: this.data.array
    });
    wx.request({
      url: 'http://8.219.238.141/user/getUserByIdentity?identity=2',
      method: 'POST',
      success: (res) => {
        this.setData({
          array: [...this.data.array, ...res.data.data]
        })
      }
    });
    this.setData({
      array: this.data.array
    });
    console.log(this.data.array);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getArray();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})